package model;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.SwingConstants;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.HighlighterFactory;

import com.alee.global.StyleConstants;
import com.alee.laf.button.WebButton;
import com.alee.laf.button.WebToggleButton;
import com.alee.laf.toolbar.WebToolBar;

public class FormatFactory {

	public static void formatHeader(JXTable table, int alignment) {
		// if (table != null) {
		// if (alignment < 0)
		// alignment = 0;
		// TableCellRenderer myRenderer = table.getTableHeader()
		// .getDefaultRenderer();
		// JLabel label = (JLabel) myRenderer;
		// label.setHorizontalAlignment(alignment);
		//
		// table.getTableHeader().setBackground(SystemColor.activeCaption);
		// }
		table.getTableHeader().addMouseListener(
				new TableColumnControlPopup(table));
	}

	public static void formatHighlighter(final JXTable table, Color color) {
		if (table != null) {
			if (color == null)
				color = Color.blue;
			table.setHighlighters(HighlighterFactory.createAlternateStriping(),
					HighlighterFactory.createComputedForegroundHighlighter(),
					HighlighterFactory.createSimpleStriping());

			table.addHighlighter(new ColorHighlighter(
					HighlightPredicate.ROLLOVER_ROW, null, color));

			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					if (table.isEnabled() && e.isPopupTrigger()) {
						Point p = e.getPoint();
						int rowNumber = table.rowAtPoint(p);
						if (rowNumber != -1 && !table.isRowSelected(rowNumber))
							table.setRowSelectionInterval(rowNumber, rowNumber);
					}
				}
			});
		}
	}

	public static WebButton createButtonToolBar(String toolTipText,
			String actionCommand, Icon icon, ActionListener action) {
		return createButtonToolBar("", toolTipText, actionCommand, icon,
				action, true);
	}

	public static WebButton createButtonToolBar(String text,
			String toolTipText, String actionCommand, Icon icon,
			ActionListener action, boolean rolloverDecoratexOnly) {
		WebButton webButton = new WebButton(text, icon, action);
		webButton.setActionCommand(actionCommand);
		webButton.setToolTipText(toolTipText);
		webButton.setRolloverDecoratedOnly(rolloverDecoratexOnly);
		webButton.setRound(StyleConstants.smallRound);
		return webButton;
	}

	public static WebToggleButton createToggleButton(String text,
			String toolTipText, String actionCommand, Icon icon,
			ActionListener action) {
		WebToggleButton tglBtn = new WebToggleButton(text, icon, action);
		tglBtn.setFont(new Font("Tahoma", Font.PLAIN, 15));
		tglBtn.setFocusable(false);
		tglBtn.setActionCommand(actionCommand);
		tglBtn.setRolloverDecoratedOnly(true);
		return tglBtn;
	}

	public static void formatToolBar(WebToolBar toolBar) {
		toolBar.setOrientation(SwingConstants.VERTICAL);
		toolBar.setFloatable(false);
		toolBar.setShadeWidth(0);
		toolBar.setSpacing(-2);
	}

}
